package vn.info.vietnaminfo.utli;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import vn.info.vietnaminfo.data.DSThanhPho;
import android.content.Context;

public class FileUtil
{
	// Get danh sách 64 tỉnh thành file txt trong thư mục Assets
	public static List<DSThanhPho> GetDSThanhPho(Context context)
	{
		try 
		{
			List<DSThanhPho> list = new ArrayList<DSThanhPho>();
			InputStream inputStream = context.getAssets().open("DSThanhPho.txt");
			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
			String line = "";
			String[] array = null;
			while ((line = reader.readLine()) != null)
			{
				array = line.split("\t");
				int index = 0;
				list.add(new DSThanhPho(array[index++], array[index++], array[index++], array[index++]));
			}
			inputStream.close();
			reader.close();
			return list;
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	public static String readFile(Context context)
	{
		InputStream inputStream = null;
		ByteArrayOutputStream output = null;
		byte[] readBuffer = new byte[128]; 	
		try
		{
			int size = 0;
			inputStream = context.getAssets().open("loaihinhkd.txt");
			output = new ByteArrayOutputStream();
			while ((size = inputStream.read(readBuffer)) > 0)
			{
				output.write(readBuffer, 0, size);
			}
			return output.toString();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if(inputStream != null) { inputStream.close(); };
				if(output != null) { output.close(); };
			}
			catch (Exception e) {}
		}
		
		return "";
	}
}